package kr.couponplay.android.activity.event;

import java.util.ArrayList;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.TitleBarHelper;
import kr.couponplay.android.activity.search.SearchSubMenuHelper;
import kr.couponplay.android.domain.BeanFactory;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.domain.deal.DealDao;
import kr.couponplay.android.domain.deal.DealList;
import kr.couponplay.android.support.AlertUtils;
import kr.couponplay.android.support.ad.AdPlatform;
import kr.couponplay.android.support.ad.AdViewFactory;
import android.annotation.SuppressLint;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.TextView;

@SuppressLint("HandlerLeak") 
public class EventActivity extends FragmentActivity {
	private EventActivity activity = this;
	private LinearLayout titleLayout;
	private ProgressDialog dataLoadingDialog;
	private AdPlatform adPlatform;
	
	public ViewPager eventListViewPager;
	private EventFragmentPagerAdapter eventListViewPagerAdapter;
	
	@Override
	public void onDestroy() {
		super.onDestroy();
	}
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.event_activity);
        
        drawTop();
        drawAdView();
    	drawBody();
    	getDataFromServer();
    }
	
    @Override
    public void onResume() {
    	super.onResume();
    	TitleBarHelper.setVisibable(titleLayout);
    	
    }
    
	private void drawAdView() {
		adPlatform = AdViewFactory.getInstance(
				AppContext.isUseAdPlatform(getApplicationContext()), AppContext.getAdPlatformType(getApplicationContext()));
		adPlatform.onCreateAdView(this);
	}    
    
	private void drawTop() {
		titleLayout = (LinearLayout) findViewById(R.id.event_title);
		TextView titleTextView = (TextView) titleLayout.findViewById(R.id.event_title_name);
		titleTextView.setText(AppContext.getEventName(getApplicationContext()) + " 기획전");
	}
	
	private void drawBody() {
		eventListViewPager =(ViewPager)findViewById(R.id.event_viewpager);
		eventListViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			@Override
			public void onPageSelected(int position) {
				SearchSubMenuHelper.drawSelectButton(SearchSubMenuHelper.get(position));
			}
        });
		eventListViewPagerAdapter = new EventFragmentPagerAdapter(activity, new ArrayList<Deal>());
        eventListViewPager.setAdapter(eventListViewPagerAdapter);
        eventListViewPager.setCurrentItem(0);
	}	
	
    private void getDataFromServer() {
    	dataLoadingDialog = ProgressDialog.show(this, null, "데이터 수신중입니다.", true, false);

        Thread thread = new Thread(new Runnable() {
            public void run() {
            	Message msg = Message.obtain();
            	try {
            		msg.obj = BeanFactory.getBean(getApplicationContext(), DealDao.class).selectEventDeals(0);
            	} catch (Exception e) {
            		msg.obj = e;
            	}
            	handler.sendMessage(msg);
            }
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
	    	if (msg.obj instanceof Throwable) {
	    		alert((Exception)msg.obj);
	    		dataLoadingDialog.dismiss();
	    		return;
	    	}
	    	DealList dealList = (DealList)msg.obj;
	    	eventListViewPagerAdapter = new EventFragmentPagerAdapter(activity, dealList.getDeals());
	    	eventListViewPager.setAdapter(eventListViewPagerAdapter);
	    	eventListViewPager.setCurrentItem(1);
		    dataLoadingDialog.dismiss();
        }
    };
    
    public void alert(Exception exception) {
        AlertUtils.errorAlert(this, exception);
    }
	
    @Override
    public void onBackPressed() {
    	TitleBarHelper.getTabHost().setCurrentTab(0);
    }
}
